package org.mili.ticketeer.service;

import org.mili.application.service.Service;
import org.mili.application.service.ServiceException;
import org.mili.ticketeer.dao.ContactMeetingDao;
import org.mili.application.dao.DaoFactory;
import org.mili.ticketeer.dao.MeetingDao;
import org.mili.application.dao.ModificationResult;
import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.dto.ContactMeeting;
import org.mili.ticketeer.dto.Meeting;
import org.mili.application.util.Lambda;

import java.sql.Timestamp;
import java.util.List;

public class MeetingService extends Service {
	private MeetingDao meetingDao = DaoFactory.getDao(MeetingDao.class);
    private ContactMeetingDao contactMeetingDao = DaoFactory.getDao(ContactMeetingDao.class);

    public Boolean assign(final Contact contact, final Meeting meeting) throws ServiceException {
        return doInService(new Lambda<Boolean>() {
            @Override
            public Boolean exec(Object... params) throws Exception {
                return contactMeetingDao.assign(contact, meeting);
            }
        });
    }

    public Boolean unassign(final Contact contact, final Meeting meeting) throws ServiceException {
        return doInService(new Lambda<Boolean>() {
            @Override
            public Boolean exec(Object... params) throws Exception {
                return contactMeetingDao.unassign(contact, meeting);
            }
        });
    }

    public List<ContactMeeting> getVisitedMeetingsFor(final Contact contact, final int year) throws ServiceException {
        List<ContactMeeting> list = doReadInService(new Lambda<List<ContactMeeting>>() {
            @Override
            public List<ContactMeeting> exec(Object... params) throws Exception {
                return meetingDao.getVisitedMeetingsFor(contact);
            }
        });
        for (int i = list.size() - 1; i >= 0; i --) {
            ContactMeeting contactMeeting = list.get(i);
            Meeting meeting = getMeetingById(contactMeeting.getMeeting());
            if (1900 + meeting.getDate().getYear() != year) {
                list.remove(i);
            }
        }
        return list;
    }

    public Meeting getMeetingById(final int meeting) throws ServiceException {
        return doReadInService(new Lambda<Meeting>() {
            @Override
            public Meeting exec(Object... params) throws Exception {
                return meetingDao.getMeetingById(meeting);
            }
        });
    }

    public List<Meeting> getMeetings() throws ServiceException {
        return doReadInService(new Lambda<List<Meeting>>() {
            @Override
            public List<Meeting> exec(Object... params) throws Exception {
                return meetingDao.getMeetings();
            }
        });
    }

    public ModificationResult write(final Meeting meeting) throws ServiceException {
        return doInService(new Lambda<ModificationResult>() {
            @Override
            public ModificationResult exec(Object... params) throws Exception {
                return meetingDao.write(meeting);
            }
        });
    }

    public Meeting findMeetingByDate(final Timestamp timestamp) throws ServiceException {
        return doReadInService(new Lambda<Meeting>() {
            @Override
            public Meeting exec(Object... params) throws Exception {
                return meetingDao.findMeetingByDate(timestamp);
            }
        });
    }

    public List<ContactMeeting> getAllMeetings(final Contact contact) throws ServiceException {
        return doReadInService(new Lambda<List<ContactMeeting>>() {
            @Override
            public List<ContactMeeting> exec(Object... params) throws Exception {
                return contactMeetingDao.getMeetingsFor(contact);
            }
        });
    }

    public List<ContactMeeting> getVisitorsForMeetings(final Meeting meeting) throws ServiceException {
        return doReadInService(new Lambda<List<ContactMeeting>>() {
            @Override
            public List<ContactMeeting> exec(Object... params) throws Exception {
                return contactMeetingDao.getVisitorsForMeetings(meeting);
            }
        });
    }
}
